/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-06-20
 * Time: 21:27
 */
//实现无头单链表的基本操作
public class ListNode {
    public int val;
    public ListNode next;
    public ListNode head;
    public int size;

    public ListNode(){};

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode node) {
        this.val = val;
        this.next = node;
    }

    //头插法
    public void addhead(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
        size++;
    }

    //尾插法
    public void addLast(int data) {
        if (head == null) {
            addhead(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode p = head;
        while (p.next != null) {
            p = p.next;
        }
        p.next = node;
        size++;
    }

    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index, int data) {
        if (index < 0 || index > size) {
            System.out.println("插入位置不合法");
            return false;
        }
        if (index == 0) {
            addhead(data);
            size++;
            return true;
        }
        ListNode node = new ListNode(data);
        ListNode p = head;
        while (--index > 0) {
            p = p.next;
        }
        node.next = p.next;
        p.next = node;
        size++;
        return true;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode p = head;
        while (p != null) {
            if (p.val == key) {
                return true;
            }
            p = p.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode p = head;
        if (p == null) {
            return;
        }
        if (p.val == key) {
            head = head.next;
            size--;
            return;
        }
        while (p.next != null) {
            if (p.next.val == key) {
                p.next = p.next.next;
                size--;
                return;
            }
            p = p.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode p = head;
        if (p == null) {
            return;
        }
        while (p.next != null) {
            if (p.next.val == key) {
                p.next = p.next.next;
                size--;
            }
            else{
                p = p.next;
            }
        }
        if (p.val == key) {
            head = head.next;
            size--;
        }
    }

    //得到单链表的长度
    public int size(){
        return size;
    }

    //打印链表内容
    public void display(){
        ListNode p=head;
        System.out.print("[ ");
        while(p!=null){
            System.out.print(p.val+" ");
            p=p.next;
        }
        System.out.println("]");
    }

    //清空链表
    public void clear(){
        if(head==null){
            return;
        }
        ListNode p1=head;
        ListNode p2=head.next;
        while(p2!=null){
            p1.next=null;
            p1=p2;
            p2=p2.next;
        }
        head=null;
        size=0;
    }
}
